package cn.iocoder.yudao.module.system.service.member;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.system.service.member.vo.MemberUserMVO;
import cn.iocoder.yudao.module.system.service.member.vo.MemberUserRespVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import static cn.iocoder.yudao.framework.common.RegExpConstant.matchesMobile;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * Member Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Value("${yudao.info.base-package}")
    private String basePackage;

    private volatile Object memberUserApi;

    @Override
    public String getMemberUserMobile(Long id) {
        Object user = getMemberUser(id);
        if (user == null) {
            return null;
        }
        return ReflectUtil.invoke(user, "getMobile");
    }
    public Object getUserByMobile(String mobile) {
        return ReflectUtil.invoke(getMemberUserApi(), "getUserByMobile", mobile);
    }
    public Object getUserByNickname(String nickname) {
        return ReflectUtil.invoke(getMemberUserApi(), "getUserByNickname", nickname);
    }
    public Object getUserByName(String name) {
        return ReflectUtil.invoke(getMemberUserApi(), "getUserByName", name);
    }
    @Override
    public String getMemberUserEmail(Long id) {
        Object user = getMemberUser(id);
        if (user == null) {
            return null;
        }
        return ReflectUtil.invoke(user, "getEmail");
    }


    private Object getMemberUser(Long id) {
        if (id == null) {
            return null;
        }
        return ReflectUtil.invoke(getMemberUserApi(), "getUser", id);
    }

    private Object getMemberUserApi() {
        if (memberUserApi == null) {
            memberUserApi = SpringUtil.getBean(ClassUtil.loadClass(String.format("%s.module.member.api.user.MemberUserApi", basePackage)));
        }
        return memberUserApi;
    }



    @Override
    public void createUser(String username,String nickname, String mobile, String password) {
        if(StrUtil.isBlank(mobile)){
            throw exception(new ErrorCode(500,"手机号不能为空"));
        }
        if(StrUtil.isBlank(nickname)){
            throw exception(new ErrorCode(500,"申请人企业不能为空"));
        }
        if(!matchesMobile(mobile)){
            throw exception(new ErrorCode(500,"手机号格式错误"));
        }
        Object memberUserDO = getUserByMobile(mobile);
        if(ObjectUtil.isNotNull(memberUserDO)){
            throw exception(new ErrorCode(500,"手机号已被注册"));
        }
        if(StrUtil.isBlank(username)){
            throw exception(new ErrorCode(500,"账号名不能为空"));
        }

        memberUserDO = getUserByName(username);
        if(ObjectUtil.isNotNull(memberUserDO)){
            throw exception(new ErrorCode(500,"账号名已被注册"));
        }
        if(StrUtil.isBlank(password)||password.length()<6){
            throw exception(new ErrorCode(500,"密码不能为空且长度不能小于6个字符"));
        }

        memberUserDO = getUserByNickname(nickname);
        if(ObjectUtil.isNotNull(memberUserDO)){
            throw exception(new ErrorCode(500,"申请人企业已被注册"));
        }
         ReflectUtil.invoke(getMemberUserApi(), "createUser", username,nickname,mobile,password);
    }

    @Override
    public void updateUser(Long id,String username,String nickname, String mobile, String password) {
        Object getUser = getMemberUser(id);
        if(ObjectUtil.isNull(getUser)){
            throw exception(new ErrorCode(500,"id错误"));
        }
        if(StrUtil.isNotBlank(mobile)&&!matchesMobile(mobile)){
            throw exception(new ErrorCode(500,"手机号格式错误"));
        }
        if(ObjectUtil.isNotNull(mobile)){
        Object memberUserDO = getUserByMobile(mobile);
        if(ObjectUtil.isNotNull(memberUserDO)){
           Long dataId = ReflectUtil.invoke(memberUserDO, "getId");
           if(!ObjectUtil.equal(id,dataId)){
            throw exception(new ErrorCode(500,"手机号已被注册"));
           }
        }
        }
        if(ObjectUtil.isNotNull(username)){
       Object  memberUserDO = getUserByName(username);
        if(ObjectUtil.isNotNull(memberUserDO)){
            Long dataId = ReflectUtil.invoke(memberUserDO, "getId");
            if(!ObjectUtil.equal(id,dataId)){
                throw exception(new ErrorCode(500,"账号名已被注册"));
            }
        }
        }

        if(StrUtil.isNotBlank(password)&&password.length()<6){
            throw exception(new ErrorCode(500,"密码长度不能小于6个字符"));
        }


        if(ObjectUtil.isNotNull(nickname)){
            Object  memberUserDO = getUserByNickname(nickname);
            if(ObjectUtil.isNotNull(memberUserDO)){
                Long dataId = ReflectUtil.invoke(memberUserDO, "getId");
                if(!ObjectUtil.equal(id,dataId)){
                    throw exception(new ErrorCode(500,"企业申请人已被注册"));
                }
            }
        }

        ReflectUtil.invoke(getMemberUserApi(), "updateUser",id, username,nickname,mobile,password);
    }
    @Override
    public void deleteUser(Long id) {
        ReflectUtil.invoke(getMemberUserApi(), "deleteUser",id);
    }

}
